package http

import (
	"im_backend/internal/logic"
	"im_backend/internal/logic/conf"
)
import "github.com/gin-gonic/gin"

type Server struct {
	engine *gin.Engine
	logic  *logic.Logic
}

func New(c *conf.HTTPServer, l *logic.Logic) *Server {
	engine := gin.New()
	engine.Use(loggerHandler, recoverHandler)
	go func() {
		if err := engine.Run(c.Addr); err != nil {
			panic(err)
		}
	}()
	s := &Server{
		engine: engine,
		logic:  l,
	}
	s.initRouter()
	return s
}

// http接口都是通过logic集群暴露
func (s *Server) initRouter() {
	group := s.engine.Group("/goim")
	// localhost:3111/goim/push/mids?operation=1000&mids=234&mids=123
	// push相关的api应该给客户端回复
	group.POST("/push/keys", s.pushKeys)
	group.POST("/push/mids", s.pushMids)
	group.POST("/push/room", s.pushRoom)
	group.POST("/push/all", s.pushAll)
	// TODO 添加、删除好友
	group.POST("/friend/request/apply", s.applyFriendReq)  // 好友申请
	group.GET("/friend/requests/get", s.getFriendReq)      // 查询好友申请
	group.POST("/friend/requests/reply", s.replyFriendReq) // 回应好友申请
	group.POST("/friend/delete", s.deleteFriend)           // 删除好友
	// TODO 进入、退出群聊
	group.POST("/room/join", s.joinRoom) // 加入房间(群聊)
	group.POST("/room/quit", s.quitRoom) // 退出房间(不再接收此房间信息)
	// TODO 消息同步  TODO 把消息同步的逻辑放在tcp层而不是http
	group.POST("/message/received", s.messageReceived) // 客户端表示已收到这个消息
	group.POST("/message/sync", s.syncMessage)         // 根据消息的序列号同步未接收的信息
}

func (s *Server) Close() {

}
